#include "register1.h"
#include "ui_register1.h"

register1::register1(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::register1)
{
    ui->setupUi(this);
    ui->password1->setEchoMode(QLineEdit::Password);;//密文输入
    ui->password2->setEchoMode(QLineEdit::Password);;//密文
}

register1::~register1()
{
    delete ui;
}

//注册页面提交按钮实现插入到数据库
void register1::on_register_submit_clicked()
{
    QApplication::setQuitOnLastWindowClosed(false);// 防止弹窗直接关闭程序

    QString name = ui->name->text();
    QString phone=ui->phone->text();
    QString email=ui->email->text();
    QString username=ui->username->text();
    QString password = ui->password1->text();
    QString password2 = ui->password2->text();
    QDateTime Datetime=QDateTime::currentDateTime();
    QString time=Datetime.toString("yyyy-MM-dd hh:mm:ss");//当前时间字符串格式
    QString flag="普通用户";//将标签设置为普通用户
    QString carid=ui->carid->text();//车牌号
    QString cartype=ui->comboBox->currentText();//汽车类型
    //查询用户是否已经存在
    QSqlQuery query3;
    QString str3 = QString("select count(*) from userinfo where username='%1';").arg(username);
    //qDebug()<<query3.lastError().text();
    if(name.isEmpty() || phone.isEmpty() || email.isEmpty() || username.isEmpty()
            || password.isEmpty() || password2.isEmpty() ||carid.isEmpty()){

        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不完整"), QMessageBox::Yes);
    }

    else if(password != password2){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("两次输入的密码不相同"), QMessageBox::Yes);
    }
    else{
        QSqlQuery query;
        QString str = QString("insert into userinfo(id,name,flag,carid,phone,email,username,password,time,fare) values(0,'%1', '%2', '%3', '%4','%5', '%6','%7','%8',0);").arg(name).arg(flag).arg(carid).arg(phone)
                .arg(email).arg(username).arg(password).arg(time);//0代表id自增
        //注册汽车信息
        QString str_2 = QString("insert into carinfo(id,carid,cartype,username) values(0, '%1', '%2','%3')").arg(carid).arg(cartype).arg(username);
        //qDebug()<<query.lastError().text();//显示数据库错误
        if(query3.exec(str3)){
            while (query3.next()) {
                if(query3.isActive()){
                    if((query3.value(0).toInt()>0))//判断是否被注册
                        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("改账号已经被注册"), QMessageBox::Yes);
                    else{
                        if(query.exec(str)&& query.exec(str_2)){            //
                                QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("注册成功！"), QMessageBox::Yes);
                                this->close();
                                emit(send_show_login());//退出注册页面返回登录页面
                        }
                        else{
                            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("注册失败！"), QMessageBox::Yes);
                        }
                    }
                }
            }
        }


    }

}
//返回
void register1::on_pushButton_clicked()
{
    this->close();
    emit(send_show_login());
}
